package rickyGame.game.action
{
	import flash.geom.Point;
	
	import ricky.game.Actor;
	import ricky.game.action.Act2D;
	import ricky.game.graphic.Graphic;
	import ricky.quadtree.Leaf;
	import ricky.utils.FrameManager;
	import ricky.utils.TimeServer;
	
	import ricky.game.Actor2D;
	import rickyGame.game.Fighter;
	import rickyGame.game.GameState;
	import rickyGame.game.SkillObject;
	import rickyGame.game.skill.SkillData;
	import rickyGame.net.MsgType;
	import rickyGame.net.WorkQueue;
	
	/**
	 * arrow的 直线行动 支持可以发射的魔法效果
	 * 不能转弯哦。
	 * 碰到就着
	 * 支持角度和多发
	 */
	public class LineAct extends Act2D
	{
		public function LineAct(actor:Actor,angle:Number,skill:SkillData)
		{
			super(actor);
			
			if(actor is SkillObject)
				this._operator=SkillObject(actor).actor;
			this._angle=angle;
			this._skill=skill;
			
		}
		
		override public function begin():void
		{
			_begin=TimeServer.now();
			FrameManager.inst.addLogic(getActor());
			var angle:Number=_angle/Common.radianToAngle;//Math.atan2(dy,dx);//角度
			_vx=Math.cos(angle)*Actor2D(_actor).speed;
			_vy=Math.sin(angle)*Actor2D(_actor).speed;
			Graphic(_actor.graphic).clothes.rotation=_angle;//*Common.radianToAngle;//角度=幅度* rangle
			_lastTime=_begin;
		}
		
		override public function action(timeline:Number):void
		{
			var dot:Number=timeline-_lastTime;
			_lastTime=timeline;
			var faraway:Number=(timeline-_begin)/1000;
			if(faraway>4)
			{
				this.complete();
				return;
			}
			
			var v1:Number=_vx*dot/1000;
			var v2:Number=_vy*dot/1000;
			var x:Number=v1+this._actor.graphic.x;
			var y:Number=v2+this._actor.graphic.y;
			
			var gx:int=Actor2D(_actor).gridX;
			var gy:int=Actor2D(_actor).gridY;
			
			this._actor.graphic.x=v1+this._actor.graphic.x;//Math.abs(targetX - x) >=5?:targetX;
			this._actor.graphic.y=v2+this._actor.graphic.y;//Math.abs(targetY - y) >=5?:targetY;
			//			
			var gx1:int=Actor2D(_actor).gridX;
			var gy1:int=Actor2D(_actor).gridY;
			
			if(this._actor.onMovingFun!=null)
				_actor.onMovingFun.call(null,_actor);
			
			if(gx!=gx1 || gy!=gy1)//格子位置改变
				onGridChange();
			
		}
		
		override public function end():void
		{
			Graphic(_actor.graphic).clothes.rotation=0;
			FrameManager.inst.removeLogic(getActor());
			super.end();
		}
		
		protected function onGridChange():void
		{
			Actor2D(_actor).lastMorton=Actor2D(_actor).morton;
			Actor2D(_actor).calcMorton();
			var cl:Leaf=GameState.inst.scene.tree.getLeaf(Actor2D(_actor).morton);
			if(cl && cl.objects && cl.objects.length>0)
			{
				var arg:Array=ActUtils.filterTarget(_operator,cl.objects,_skill);
				
				if(arg.length>=0)
				{
//					WorkQueue.instance.pushRecive(MsgType.SERVER_SKILL_EFFECT_TARGETS,_operator,arg,this._skill);
					if(_skill.parameter.indexOf("follow")>=0) //如果是跟踪就消失
						complete();
				}
			}
		}
		
		
		//
		private var _angleRadians:Number;//以弧度为单位的角度
		private var _operator:Actor;//操作者
		private var _angle:Number;//目标方向
		private var _skill:SkillData;//技能数据
		private var _lastTime:Number;//
		private var _begin:Number;
		private var _vx:Number=0;
		private var _vy:Number=0;
	}
}